﻿$(document).ready(function () {
    function PackageItem(data) {
        this.type = ko.observable();
        this.path = ko.observable();
        this.includes = ko.observable();
        this.excludes = ko.observable();

        if (data) {
            this.update(data);
        }
    }

    PackageItem.prototype = {
        update: function (data) {
            if (typeof data.Type !== 'undefined') {
                this.type(data.Type);
            }
            if (typeof data.Path !== 'undefined') {
                this.path(data.Path);
            }
            if (typeof data.Includes !== 'undefined') {
                this.includes(data.Includes);
            }
            if (typeof data.Excludes !== 'undefined') {
                this.excludes(data.Excludes);
            }
        },
        getName: function () {
            return this.path().substring(this.path().lastIndexOf('\\') + 1);
        },
        getIcon: function () {
            switch (this.type()) {
                case 0:
                    return 'images/folder.png';
                default:
                    return 'images/file.png';
            }
        }
    };

    function Package() {
        var self = this;

        this.cached = ko.observable(true);

        this.filename = ko.observable();
        this.input = ko.observable();
        this.output = ko.observable();
        this.zip = ko.observable();
        this.includes = ko.observable();
        this.excludes = ko.observable();
        this.items = ko.observableArray();
        this.packresult = {
            status: ko.observable(),
            message: ko.observable(),
            reset: function () {
                this.status(0);
                this.message("");
            },
            open: function () {
                Local.ajax('open-folder', self.output());
            }
        };

        this.clear = function (data, e) {
            $(document).trigger('click');
            e.stopPropagation();

            $('#deleteDialog').inlineDialog({
                method: 'show',
                target: e.target,
                onbutton: function (index) {
                    switch (index) {
                        case 0:
                            this.hide();
                            self.items([]);
                            break;
                        case 1:
                            this.hide();
                            break;
                    }
                }
            });
        };
        this.remove = function (data, e) {
            $(document).trigger('click');
            e.stopPropagation();

            $('#deleteDialog').inlineDialog({
                method: 'show',
                target: e.target,
                onbutton: function (index) {
                    switch (index) {
                        case 0:
                            this.hide();
                            self.items.remove(data);
                            break;
                        case 1:
                            this.hide();
                            break;
                    }
                }
            });
        };
        this.add = function (data, e) {
            $(document).trigger('click');
            e.stopPropagation();

            $('#addDialog').inlineDialog({
                method: 'show',
                target: e.target,
                onbutton: function (index) {
                    switch (index) {
                        case 0:
                            this.hide();
                            break;
                        case 1:
                            this.hide();
                            break;
                    }
                }
            });
        };
        this.extra = function (data, e) {
            $(document).trigger('click');
            e.stopPropagation();

            $('#extraDialog').inlineDialog({
                method: 'show',
                target: e.target,
                onbutton: function (index) {
                    this.hide();
                }
            });
        };
        this.update = function (data) {
            if (typeof data.Filename !== 'undefined') {
                this.filename(data.Filename);
            }
            if (typeof data.Input !== 'undefined') {
                this.input(data.Input);
                this.updateInput();
            }
            if (typeof data.Output !== 'undefined') {
                this.output(data.Output);
            }
            if (typeof data.Zip !== 'undefined') {
                this.zip(data.Zip);
            }
            if (typeof data.Includes !== 'undefined') {
                this.includes(data.Includes);
            }
            if (typeof data.Excludes !== 'undefined') {
                this.excludes(data.Excludes);
            }

            if (typeof data.Items !== 'undefined') {
                var items = [];
                for (var i = 0; i < data.Items.length; i++) {
                    items.push(new PackageItem(data.Items[i]));
                }
                this.items(items);
            }
        };
        this.pack = function (item, e) {
            e.stopPropagation();

            var pack = this.createDataObj();
            var self = this;

            this.packresult.reset();

            $(e.target).guard(function () {
                Local.ajax('pack', pack, function (data) {
                    if (data) {
                        self.packresult.status(data.status);
                        self.packresult.message(data.message);
                        $('#packResultDialog').inlineDialog({
                            method: 'show',
                            target: e.target
                        });
                    }
                }, self);
            });
        };
        this.save = function (item, e) {
            e.stopPropagation();

            var pack = this.createDataObj();
            var self = this;
            $(e.target).guard(function () {
                Local.ajax('save', pack, function (data) {
                    if (data) {
                        self.filename(data);
                        $('#saveResultDialog').inlineDialog({
                            method: 'show',
                            target: e.target
                        });
                    }
                }, self);
            });
        };
        this.openSavedFile = function (data, e) {
            Local.ajax('open-file', self.filename());
        };

    }
    Package.prototype = {
        init: function () {
            this.input('c:\\temp\\dsuite-P-dao');
            this.output('c:\\temp\\pack');
            this.zip('20120611');
            this.items([new PackageItem({
                Type: 'file',
                Path: 'META-INF\\MANIFEST.MF'
            }), new PackageItem({
                Type: 'folder',
                Path: 'jp'
            })]);
            for (var i = 0; i < 20; i++) {
                this.items.push(new PackageItem({
                    Type: 'file',
                    Path: 'META-INF\\MANIFEST.MF\\123412341234\\1234213412341234\\12341234' + i
                }));
            }
        },
        updateInput: function () {
            var self = this;
            this.cached(false);
            Local.ajax('change-folder', this.input(), function (data) {
                self.cached(true);
            }, this, true);
        },
        reset: function () {
            Local.ajax('new', null, function (data) {
                this.update(data);
            }, this);
        },
        open: function (item, e) {
            var self = this;
            $(e.target).guard(function () {
                Local.ajax('open', null, function (data) {
                    if (data) {
                        this.update(data);
                    }
                }, self);
            });
        },
        createDataObj: function () {
            var obj = {
                Input: this.input(),
                Output: this.output(),
                Zip: this.zip(),
                Includes: this.includes(),
                Excludes: this.excludes(),
                Items: []
            };

            var items = this.items();
            for (var i = 0; i < items.length; i++) {
                obj.Items.push({
                    Type: items[i].type(),
                    Path: items[i].path(),
                    Includes: items[i].includes(),
                    Excludes: items[i].excludes()
                });
            }

            return obj;
        }
    };

    function Search(package) {
        var self = this;

        this.package = package;

        this.input = ko.observable();
        this.items = ko.observableArray();
        this.totalCount = ko.observable();

        this.search = function () {
            Local.ajax('search', { input: self.input(), limit: 100 }, function (data) {
                if (data) {
                    var items = [];
                    for (var i = 0; i < data.data.length; i++) {
                        items.push(new PackageItem(data.data[i]));
                    }
                    this.totalCount(data.count);
                    this.items(items);
                }
            }, self);
        };
        this.select = function (data, e) {
            self.package.items.push(data);
            $('#addDialog').inlineDialog({
                method: 'hide'
            });
        };
    }

    var package = new Package();
    package.init();
    package.reset();

    var search = new Search(package);

    ko.applyBindings(package, $('#package')[0]);
    ko.applyBindings(search, $('#addDialog')[0]);

    $('#deleteDialog').inlineDialog({
        buttons: ['Đồng ý', 'Hủy bỏ']
    });
    $('#addDialog').inlineDialog();
    $('#extraDialog').inlineDialog({
        buttons: ['Đóng']
    });
    $('#commonDialog').inlineDialog({
        buttons: ['Đóng']
    });
    $('#loadingDialog').inlineDialog();
    $('#packResultDialog').inlineDialog();
    $('#saveResultDialog').inlineDialog();

    function alertDialog(caption, message) {
        $('#commonDialog thead td').text(caption);
        $('#commonDialog tbody td').text(message);
        $('#commonDialog').inlineDialog({
            method: 'show',
            target: $('#toolbar')[0],
            onbutton: function (index) {
                this.hide();
            }
        });
    }

    $(window).resize(function () {
        var target = $('#itemlist');
        target.height($(window).height() - target.offset().top - 40);

        $('.vscroll').each(function () {
            $(this).width($(this).closest('div').width());
        });
    });
});

$(window).load(function () {
    $(window).trigger('resize');
});